/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import utilities.ImageScaler;

/**
 *
 * @author icdev
 */
public class PrintPreviewer extends JPanel {

    BufferedImage image;

    public PrintPreviewer(BufferedImage image) {
        this.image = image;
        this.addMouseListener(new MouseActions());
    }
    String zoomStatus;

    public void setZoomStatus(String string) {
        zoomStatus = string;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int x = 30;
        int y = 50;
        g.drawImage(ImageScaler.scaleImage(image, BufferedImage.TYPE_INT_RGB, this.getSize().width - 60, this.getSize().height - 100), x, y, this);
    }

    class MouseActions extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            /*
             * In this function image scale is been handled
             * according to selected type even zoom in or out or fit back
             */
            int x = PrintPreviewer.this.getSize().width;
            int y = PrintPreviewer.this.getSize().height;
            if (PrintPreviewer.this.zoomStatus == "zoomin") {
                x += 50;
                y += 50;
            } else if (PrintPreviewer.this.zoomStatus == "zoomout") {
                if (PrintPreviewer.this.getSize().width >= 500 && PrintPreviewer.this.getSize().height >= 600) {
                    x -= 50;
                    y -= 50;
                }
            }
            Component cont = PrintPreviewer.this.getParent();

            Dimension dim = new Dimension(x, y);
            PrintPreviewer.this.setSize(dim);
            PrintPreviewer.this.setPreferredSize(dim);

            int a = cont.getSize().width / 2 - PrintPreviewer.this.getSize().width / 2;
            int b = cont.getSize().height / 2 - PrintPreviewer.this.getSize().height / 2;

            PrintPreviewer.this.setLocation(a, b);

        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    }
}
